<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let type_map = this.field_type(this.field_type.CAN_PARAM, 'label');
    let validate_map = {'["nullable"]':'否','["required"]':'是'};
    let forms_config = {
      name: {label:'参数名',name:'name',type:'text_one',tips:'参数名',validate:['max:50']},
      tips: {label:'提示',name:'tips',type:'text_one',tips:'参数他提示信息'},
      type: {label:'输入类型',name:'type',type:'select_one',config:type_map,tips:'选择输入的组件类型',value:''},
      config: {label:'组件配置',name:'config',type:'text_more',tips:''},
      validate: {label:'验证规则',name:'validate',type:'validate_rule',value:'[]',tips:''},
      value: {label:'默认值',name:'value',type:'text_one',tips:'默认值'},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/param/edit?id=`+this.id : '/admin/param/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('index', '$refs.list_url.list');
          iframe.hide('edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })

    }
  },
  watch: {
    'config.type.value'(newValue){
      s94Mjs.each(field_type.component_config(newValue), (v,k)=>{
        this.config.config[k] = v;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/param/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
  }
}
</script>
